package com.fan.leetcode;

import org.junit.jupiter.api.Test;

/**
 * 10. 正则表达式匹配
 *
 * @author zhangFan
 * @since 2021/9/6 10:47
 */
public class Question10 {
    @Test
    void test() {
        System.out.println("isMatch() = " + isMatch("aaa", "aaaa"));
    }

    /**
     * 暴力解题
     *
     * @param s 字符串
     * @param p 字符规律
     * @return 是否匹配
     */
    /*public boolean isMatch(String s, String p) {
        return s.matches(p);
    }*/
    public boolean isMatch(String s, String p) {
        char temp = 0;
        int i = 0, j = 0;
        while (true) {
            if (p.charAt(j) == '.' || s.charAt(i) == p.charAt(j)) {
                temp = p.charAt(j);
                if (i == s.length() - 1) {
                    if (j == p.length() - 1) {
                        break;
                    }
                    j++;
                    continue;
                }
                i++;
                j++;
            } else if (j < p.length() - 1 && p.charAt(j + 1) == '*') {
                if (j < p.length() - 2) {
                    j += 2;
                } else {
                    j++;
                    break;
                }
            } else if (p.charAt(j) == '*' && (s.charAt(i) == temp || temp == '.')) {
                if (i == s.length() - 1) {
                    if (j == p.length() - 1) {
                        break;
                    }
                    j++;
                    continue;
                }
                i++;
            } else {
                j++;
            }
        }
        return i == s.length() - 1 && j == p.length() - 1;
    }
}
